﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data.Objects;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wRouteRegistry.xaml
    /// </summary>
    public partial class wRouteRegistry : Window
    {
        routelistEntities db;
        IEditableCollectionView vw;



        public routelistEntities ContextDB
        {
            get { return (routelistEntities)GetValue(ContextDBProperty); }
            set { SetValue(ContextDBProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContextDB.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContextDBProperty =
            DependencyProperty.Register("ContextDB", typeof(routelistEntities), typeof(wRouteRegistry), new UIPropertyMetadata(null));




        public Routes Route
        {
            get { return (Routes)GetValue(RouteProperty); }
            set { SetValue(RouteProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Route.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RouteProperty =
            DependencyProperty.Register("Route", typeof(Routes), typeof(wRouteRegistry), new UIPropertyMetadata(null));

        

        public wRouteRegistry()
        {
            InitializeComponent();
            //db = new routelistEntities();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (null == ContextDB)
            {
                db = new routelistEntities();
            }
            else
            {
                buttonSelect.Visibility = System.Windows.Visibility.Visible;
                db = ContextDB;
            }
            var query = from o in db.Routes orderby o.number select o;
            CollectionViewSource src = (CollectionViewSource)FindResource("sourceRoute");
            src.Source = query;
            vw = (IEditableCollectionView)src.View;
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            wRouteRegistryDialog dlg = new wRouteRegistryDialog();
            Routes route = (Routes)vw.AddNew();
            route.del = false;
            route.number = "<введите номер маршрута>";
            route.name = "<введите наименование маршрута>";

            dlg.DataContext = route;
            dlg.DBContext = db;
            if (dlg.ShowDialog() == true)
            {
                db.Routes.AddObject(route);
                Save();
                Refresh(route);
            }
            else
            {
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            Routes item = listView1.SelectedItem as Routes;
            if (null == item) return;
            item.del = !item.del;
            Save();
        }

        private void Refresh(Routes item)
        {
            Routes curItem = item;
            IQueryable<Routes> query = from o in db.Routes orderby o.number select o;
            CollectionViewSource src = (CollectionViewSource)FindResource("sourceRoute");
            src.Source = query;
            vw = (IEditableCollectionView)src.View;
            listView1.ScrollIntoView(curItem);
            listView1.SelectedItem = curItem;
        }

        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            ObjectStateEntry entry;
            Routes item = listView1.SelectedItem as Routes;
            if (null == item) return;
            wRouteRegistryDialog dlg = new wRouteRegistryDialog();
            dlg.DBContext = db;
            dlg.DataContext = item;
            if (dlg.ShowDialog() == true)
            {
                Save();
                Refresh(item);
                //db.Routes.Context.Refresh(RefreshMode.StoreWins, item);
            }
            else
            {
                db.ObjectStateManager.TryGetObjectStateEntry(item, out entry);
                if (entry.State == System.Data.EntityState.Modified)
                {
                    db.Routes.Context.Refresh(RefreshMode.StoreWins, item);
                }
            }
        }

        private void Save()
        {
            try
            {
                db.SaveChanges();
               // MessageBox.Show("OK");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonSelect_Click(object sender, RoutedEventArgs e)
        {
            CollectionViewSource src = (CollectionViewSource)FindResource("sourceRoute");
            Route = src.View.CurrentItem as Routes;
            try
            {
                DialogResult = true;
            }
            catch (Exception)
            {
            }
        }
    }
}
